/*
 * common.h
 *
 *	\brief This files contains miscellaneous functions, mostly
 *	helper functions that can be used anywhere in our program.
 *
 *  Created on: Aug 30, 2012
 *      Author: Adrian Ortega Garcia
 *       email: elfus0.1@gmail.com
 */

#ifndef COMMON_H_
#define COMMON_H_

#include <stdio.h>
#include "elf_t.h"

#define MAX_FILE_SIZE	0x06400000 //!< 100Mb
#define MIN_FILE_SIZE	0x00000400 //!< 1Kb

typedef enum endianess{
	LIL_ENDIAN = 0,
	BG_ENDIAN
}endianess_t;

/**
 * \brief Implements a linked list of generic pointers.
 *
 * \details It is meant to be used to keep track of all the pointers returned
 * by successful calls to malloc function.
 */
typedef struct item{
	void	*ptr;
	struct item	*next;
}item;

/**
 * \brief Adds an item to the end of the list.
 *
 * \param[in] first_item Pointer to the first item in the list.
 * \param[in] new_ptr Pointer to be added at the end of the list.
 */
void	list_add_item(item *first_item, item *new_ptr);

/**
 * \brief Frees all the elements in the list.
 *
 * \param[in] first_item Pointer to the first item in the list.
 */
void	list_free(item *first_item);

/**
 * \brief Feature 1: Prints the binary content of the given file.
 *
 * \note This function assumes file pointed to by fp is already opened.
 *
 * \param[in] fp File pointer to a valid file.
 */
void	print_binary_content(FILE * fp);


//! \brief Helper function to check procesor's endianess
endianess_t check_endianess(void);

/**
 * \brief Checks the given char is bnetween 33 and 126 in the ascii table
 *
 * \param[in] a Character to evaluate
 *
 * \retval The a Character if valid, otherwise a dot '.'
 */
char	is_valid_char(char a);

/**
 * \brief Opens the file and checks if it has the valid file size, between 1K
 * to 100mb.
 *
 * \retval Returns a valid File Decriptor, otherwise returns 0 on error.
 */
FILE *		open_file(const char *name);

/**
 * \brief Prints all the available features in the dissassembler.
 */
void	print_available_features(void);


#define STR(s) #s

/**
 * \defgroup HelperFunctions Help functions to print strings according to
 * the receiving argument.
 * @{
 */
char *	elf_type(int type);
char *	machine_type(int type);
char *	elf_os_abi(int type);
char *	phdr_type(Elf64_Word type);
char *	phdr_flag_type(Elf64_Word type);
char *	section_type(int type);
/**@}*/
#endif /* COMMON_H_ */
